﻿using System;
using System.Collections.Generic;

namespace Common
{
    public class ClassData
    {
        public ClassData()
        {
            Vehicles = new List<ClassVehicle>(2);
        }

        public int ID
        {
            set;
            get;
        }

        public string Code
        {
            set;
            get;
        }

        public Guid Brigade
        {
            set;
            get;
        }

        public int CaseType
        {
            set;
            get;
        }

        public Guid User
        {
            set;
            get;
        }

        public string Contact
        {
            set;
            get;
        }

        public DateTime TimeHappen
        {
            set;
            get;
        }

        public DateTime TimeEntrust
        {
            set;
            get;
        }

        public DateTime TimeArrange
        {
            set;
            get;
        }

        public List<ClassVehicle> Vehicles
        {
            set;
            get;
        }

        /// <summary>
        /// 判断两个数据是否同一个案子
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool IsSame(ClassData data)
        {
            if(Brigade == data.Brigade & Contact == data.Contact & TimeHappen == data.TimeHappen)
            {
                bool isVehicleEq = true;
                for (int i = 0; i < Vehicles.Count; i++)
                {
                    if(Vehicles[i].Number != data.Vehicles[i].Number)
                    {
                        isVehicleEq = false;
                    }
                }
                if (isVehicleEq)
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsEqual(ClassData data)
        {
            string a, b;
            if (this == null || IsEmpty())
            {
                a = "";
            }
            else
            {
                a = CodingToString();
            }
            if(data == null || data.IsEmpty())
            {
                b = "";
            }
            else
            {
                b = data.CodingToString();
            }
            /////////////////////////////////////////
            return a.Equals(b);
        }

        public override bool Equals(object obj)
        {
            var other = obj as ClassData;
            return other != null && CodingToString() == other.CodingToString();
        }

        public static bool operator ==(ClassData dataL, ClassData dataR)
        {
            if(ReferenceEquals(dataL, dataR))
            {
                return true;
            }
            return dataL.Equals(dataR);
        }

        public static bool operator !=(ClassData dataL, ClassData dataR) => !(dataL == dataR);

        public override int GetHashCode()
        {
            return CodingToString().GetHashCode();
        }

        public string CodingToString()
        {
            string s = "";
            if(IsEmpty())
            {
                return s;
            }
            s += ID.ToString() + "♠";
            s += Code + "♠";
            s += CaseType.ToString() + "♠";
            s += Brigade.ToString() + "♠";
            s += Contact + "♠";
            s += User.ToString() + "♠";
            s += TimeHappen.ToString("yyyy/MM/dd HH:mm") + "♠";
            if(TimeEntrust == null)
            {
                TimeEntrust = DateTime.MinValue;
                s += DateTime.MinValue.ToString("yyyy/MM/dd") + "♠";
            }
            else
            {
                s += TimeEntrust.ToString("yyyy/MM/dd") + "♠";
            }
            s += TimeArrange.ToString("yyyy/MM/dd HH:mm:ss") + "♠";
            string st = null;
            foreach (ClassVehicle v in Vehicles)
            {
                if((v.Number == null || v.Number == string.Empty) & 
                    (v.Brand == null || v.Brand == string.Empty) &
                    (v.Type == null || v.Type == string.Empty))
                {
                    continue;
                }
                st += v.Coding2String('♣') + "♥";
            }
            s += st.Substring(0, st.Length - 1);
            return s;
        }

        public void DecodingFromString(string message)
        {
            string[] str = message.Split('♠');
            ID = Int32.Parse(str[0]);
            Code = str[1];
            CaseType = Int32.Parse(str[2]);
            Brigade = new Guid(str[3]);
            Contact = str[4];
            User = new Guid(str[5]);
            TimeHappen = DateTime.Parse(str[6]);
            if(str.Length < 10)
            {
                TimeEntrust = DateTime.MinValue;
                TimeArrange = DateTime.Parse(str[7]);
                string[] st = str[8].Split('♥');
                foreach (string temp in st)
                {
                    if (temp != null)
                    {
                        ClassVehicle vehicle = new ClassVehicle();
                        vehicle.DecodingFromString(temp, '♣');
                        if(vehicle.Number == null || vehicle.Number == string.Empty)
                        {
                            continue;
                        }
                        Vehicles.Add(vehicle);
                    }
                }
            }
            else
            {
                TimeEntrust = DateTime.Parse(str[7]);
                TimeArrange = DateTime.Parse(str[8]);
                string[] st = str[9].Split('♥');
                foreach (string temp in st)
                {
                    if (temp != null)
                    {
                        ClassVehicle vehicle = new ClassVehicle();
                        vehicle.DecodingFromString(temp, '♣');
                        if (vehicle.Number == null || vehicle.Number == string.Empty)
                        {
                            continue;
                        }
                        Vehicles.Add(vehicle);
                    }
                }
            }
        }

        public bool IsEmpty()
        {
            if(this == null)
            {
                return true;
            }
            if ((Code == null | Code == string.Empty) & (Contact == null | Contact == string.Empty))
            {
                return true;
            }
            return false;
        }

        public void Clone(ClassData data)
        {
            if (data == null)
            {
                ID = 0;
                Code = null;
            }
            else
            {
                ID = data.ID;
                Code = data.Code;
                CaseType = data.CaseType;
                Brigade = data.Brigade;
                Contact = data.Contact;
                User = data.User;
                TimeHappen = data.TimeHappen;
                TimeEntrust = data.TimeEntrust;
                TimeArrange = data.TimeArrange;
                Vehicles.Clear();
                foreach(ClassVehicle veh in data.Vehicles)
                {
                    ClassVehicle v = new ClassVehicle();
                    v.Clone(veh);
                    Vehicles.Add(v);
                }
            }
        }
    }
}
